package com.github.east196.terminator.xtend.bot

import com.google.common.io.Files
import java.io.File
import java.util.List
import org.dom4j.Attribute
import org.dom4j.Document
import org.dom4j.Element
import org.dom4j.Node
import org.dom4j.io.SAXReader
import com.github.east196.terminator.xtend.model.Field

class FXmlBot {
	var static i = 1

	def static void main(String[] args) {
		
		var path = '''D:\workspace\east196\java\terminator\terminator-fx\src\main\resources\fxml\initializers.fxml'''
		Files.copy(new File(path), new File(path + ".bak"))
		handleXml(path)
	}

	def static handleXml(String path) {
		var SAXReader reader = new SAXReader()
		// 通过reader对象的read方法加载fxml文件,获取docuemnt对象。
		val file = new File(path)
		val fileName = file.name.split("\\.").get(0)
		var Document document = reader.read(file)
		// 通过document对象获取根节点
		var Element root = document.getRootElement()
		
		// 提取fxml中的fx:id
		var List<String> fxids = FXmlBot.collectFxIds(root)
//		fxids.forEach[println(it)]
		
		// 提取fxml中的import
		var List<String> imports = collectImports(document)
		
		var bean = geneBean(fxids,imports,fileName)
		print(bean)
				var ctl = geneController(fxids,imports,fileName)
		print(ctl)
	}

	def static List<String> collectFxIds(Element root) {
		var List<String> fxids = newArrayList()
		System.out.println("=====开始遍历=====" + root.name)
		// 获取book的属性名以及 属性值
		var List<Attribute> rootAttrs = root.attributes()
		for (Attribute attr : rootAttrs) {
			System.out.println('''属性名：«attr.getName()» «attr.qualifiedName»--属性值：«attr.getValue()»''')
			if (attr.qualifiedName == "fx:id") {
				fxids.add(attr.getValue() + "__" + root.name)
			}
		}
		var itt = root.elementIterator()
		while (itt.hasNext()) {
			var Element bookChild = (itt.next() as Element)
			System.out.println('''节点名：«bookChild.getName()»''')
			fxids.addAll(FXmlBot.collectFxIds(bookChild))
		}
		System.out.println("=====结束遍历=====" + root.name)
		fxids
	}

	def static List<String> collectImports(Document document) {
		var List<String> imports = newArrayList()
		System.out.println("=====开始遍历=====")
		val nodeIterator = document.nodeIterator
		while (nodeIterator.hasNext()) {
			var Node node = (nodeIterator.next() as Node)
			if (node.getName() == "import") {
				System.out.println('''节点名：«node.getName()» «node.stringValue»''')
				imports.add(node.stringValue)
			}
		}
		System.out.println("=====结束遍历=====")
		imports
	}
	
	def static geneController(List<String> fxids,List<String> imports,String fileName){
		var klassType = fileName.toFirstUpper
		var fields=fxids.map[fxid|
			print(fxid)
			var field = new Field()
			field.name=fxid.split("\\__").get(0)
			field.type=fxid.split("\\__").get(1)
			return field
		]
		print(fields)
		print("/n")
		'''

			
			«FOR f : fields»
			
			@FXML
			private «f.type» «f.javaName»;
			«ENDFOR»
		
		'''
	}
	
		
	def static geneBean(List<String> fxids,List<String> imports,String fileName){
		var klassType = fileName.toFirstUpper
		var fields=fxids.map[fxid|
			print(fxid)
			var field = new Field()
			field.name=fxid.split("\\__").get(0)
			field.type=fxid.split("\\__").get(1)
			return field
		]
		print(fields)
		print("/n")
		'''
		
		import org.apache.commons.lang3.builder.HashCodeBuilder;
		import org.apache.commons.lang3.builder.EqualsBuilder;
		import org.apache.commons.lang3.builder.ToStringBuilder;
		import org.apache.commons.lang3.builder.ToStringStyle;
		
		import cn.edu.scau.biubiusuisui.annotation.FXEntity;
		import cn.edu.scau.biubiusuisui.annotation.FXField;
		
		«FOR i :imports»
		import «i»;
		«ENDFOR»
		
		@FXEntity
		public class «klassType» {
			
			«FOR f : fields»
			
			@FXField
			private «f.type» «f.javaName»;
			«ENDFOR»
			
			«FOR f : fields»
			public «f.type» get«f.javaName.toFirstUpper»() {
				return «f.javaName»;
			}

			public void set«f.javaName.toFirstUpper»(«f.type» «f.javaName») {
				this.«f.javaName» = «f.javaName»;
			}
			
			«ENDFOR»
			@Override 
			public int hashCode() {
				return HashCodeBuilder.reflectionHashCode(this);
			}
		
			@Override 
			public boolean equals(Object other) {
				return EqualsBuilder.reflectionEquals(this, other);
			}

			@Override 
			public String toString() {
				return ToStringBuilder.reflectionToString(this,ToStringStyle.DEFAULT_STYLE);
			}
			
		}
		'''
	}

}
